<template>
  <div class="dashboard-container">
    <div class="dashboard-text">{{ name }}，你好！</div>
    <div class="dashboard-text">欢迎登陆 EASY_EXAM</div>
    <component :is="currentRole" />

    <el-dialog title="完善组织信息" :visible.sync="modifyVisible">
      <el-form
        :model="orgInfo"
        ref="orgInfo"
        label-width="100px"
        label-position="right"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="公司">
          <el-select
            @change="selectDeptList"
            v-model="orgInfo.companyId"
            class="filter-item"
            placeholder="请选择..."
          >
            <el-option
              v-for="item in companyList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门">
          <el-select v-model="orgInfo.deptId" class="filter-item" placeholder="请选择...">
            <el-option v-for="item in deptList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="modifyVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import adminDashboard from "./admin";
import { getAllCompany, getDeptListByCompanyId } from "@/api/org/org";
import { updateOrg } from "@/api/login";
import store from "../../store";

export default {
  name: "Dashboard",
  components: { adminDashboard },
  data() {
    return {
      modifyVisible: false,
      currentRole: "adminDashboard",
      orgInfo: {
        companyId: null,
        deptId: null
      },
      companyList: [],
      deptList: []
    };
  },
  methods: {
    selectDeptList(companyId) {
      getDeptListByCompanyId(companyId).then(res => {
        if (res.data) {
          this.deptList = res.data;
        } else {
          this.$message.error(res.code);
        }
      });
    },

    submitForm() {
      // 保存
      this.$refs["orgInfo"].validate(valid => {
        if (valid) {
          // this.categoryForm.roleIds=this.$refs.multipleTree.getCheckedKeys();
          let companyId = this.orgInfo.companyId;
          let deptId = this.orgInfo.deptId;
          updateOrg(companyId,deptId).then(res => {
            this.modifyVisible = false;
          });
        } else {
          return false;
        }
      });
    }
  },
  computed: {
    ...mapGetters(["name", "roles"])
  },
  mounted() {
    if (store.getters.companyId === null) {
      this.modifyVisible = true;
      getAllCompany().then(res => {
        if (res.data) {
          this.companyList = res.data;
        } else {
          this.$message.error(res.code);
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
    text-align: center;
  }
  &-text {
    font-size: 30px;
    line-height: 86px;
  }
}
</style>
